package cn.gailvlun.gll.presentation.charge

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.Context
import android.content.Intent
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import cn.gailvlun.gll.R
import cn.gailvlun.gll.databinding.ActivityChargeListBinding
import cn.gailvlun.gll.net.ListRes
import cn.gailvlun.gll.net.NewQueryParam
import cn.gailvlun.gll.net.pay.PayOrderModel
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.charge.adapter.ChargeListAdapter
import cn.gailvlun.gll.presentation.charge.model.Outcome
import cn.gailvlun.gll.presentation.charge.vm.ChargeListViewModel
import cn.gailvlun.gll.util.DialogUtil
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener

class ChargeListActivity : BaseActivity(), OnLoadmoreListener {
    companion object {
        fun openActivity(context: Context) {
            context.startActivity(Intent(context, ChargeListActivity::class.java))
        }
    }

    private lateinit var mBinding: ActivityChargeListBinding
    private lateinit var mAdapter: ChargeListAdapter
    private lateinit var mLayoutManager: LinearLayoutManager

    private var mViewModel: ChargeListViewModel? = null

    override fun initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_charge_list)

        mViewModel = ViewModelProviders.of(this).get(ChargeListViewModel::class.java)

        mAdapter = ChargeListAdapter(mContext)
        mBinding.rv.adapter = mAdapter

        mLayoutManager = LinearLayoutManager(mContext)
        mBinding.rv.layoutManager = mLayoutManager

        mBinding.srl.isEnableRefresh = false
        mBinding.srl.isEnableLoadmore = false
        mBinding.srl.setOnLoadmoreListener(this)
    }

    override fun createHandler() {
        mBinding.nbv.setOnBackListener {
            onBackPressed()
        }
    }

    override fun loadData(savedInstanceState: Bundle?) {
        mViewModel?.orderOutcome?.observe(this, object : Observer<Outcome<ListRes<PayOrderModel>>> {
            override fun onChanged(t: Outcome<ListRes<PayOrderModel>>?) {
                when (t) {
                    is Outcome.Progress -> {
                        if (t.loading) {

                        } else {
                            mBinding.srl.finishLoadmore()
                        }
                    }
                    is Outcome.Success -> {
                        if (t.data.first) {
                            mAdapter.clear()
                        }

                        mAdapter.addAll(t.data.results)

                        mBinding.srl.isEnableLoadmore = t.data.isNext
                    }
                    is Outcome.Failure -> {
                        DialogUtil.showError(mContext, t.e)
                    }
                }
            }

        })

        mViewModel?.getOrderList(null, null)
    }

    val queryParam = NewQueryParam()

    override fun onLoadmore(refreshlayout: RefreshLayout?) {
        queryParam.direction = NewQueryParam.next
        queryParam.lastId = mAdapter.getLastId()
        mViewModel?.getOrderList(queryParam.direction, queryParam.lastId)
    }

}
